package com.qiantu.phone.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.LocationData;
import com.qiantu.phone.event.LocationEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * 包名： com.amap.locationservicedemo
 * <p>
 * 创建时间：2016/10/27
 * 项目名称：LocationServiceDemo
 *
 * @author guibao.ggb
 * @email guibao.ggb@alibaba-inc.com
 * <p>
 * 类说明：后台服务定位
 *
 * <p>
 * modeified by liangchao , on 2017/01/17
 * update:
 * 1. 只有在由息屏造成的网络断开造成的定位失败时才点亮屏幕
 * 2. 利用notification机制增加进程优先级
 * </p>
 */
public class LocationService extends Service {

    private String TAG = "LocationService";

    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.d("LocationService", "service start!");
        startLocation();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        stopLocation();
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 启动定位
     */
    private void startLocation() {
        Log.d("LocationService", "start location!!");
        stopLocation();
//        if (null == mLocationClient) {
            mLocationClient = new AMapLocationClient(this.getApplicationContext());
            mLocationOption = new AMapLocationClientOption();
            mLocationOption.setOnceLocation(true);
            mLocationOption.setOnceLocationLatest(true);
            mLocationOption.setLocationCacheEnable(false);
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            // 地址信息
            mLocationOption.setNeedAddress(true);
            mLocationClient.setLocationOption(mLocationOption);
            mLocationClient.setLocationListener(locationListener);
//        }
        Log.d("LocationService", "start location!!");
        mLocationClient.startLocation();
    }

    /**
     * 停止定位
     */
    private void stopLocation() {
        if (null != mLocationClient) {
            mLocationClient.stopLocation();
        }
    }

    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            Log.d("LocationService", "onLocationChanged:" + aMapLocation.getAddress() + " latitude:" + aMapLocation.getLatitude() + " longitude:" + aMapLocation.getLongitude());
            if (aMapLocation.getLatitude() <= 0.001f || aMapLocation.getLongitude() <= 0.001f) {
                return;
            }
            LocationData location = new LocationData();
            location.locationCN = aMapLocation.getAddress();
            location.latitude = aMapLocation.getLatitude();
            location.longitude = aMapLocation.getLongitude();
            String adCode = aMapLocation.getAdCode();
            location.province = aMapLocation.getProvince();
            location.provinceId = adCode.substring(0, 2);
            location.city = aMapLocation.getCity();
            location.cityId = adCode.substring(2, 4);
            location.district = aMapLocation.getDistrict();
            location.districtId = adCode.substring(4, 6);
            AppApplication.getInstance().currentLocation = location;
            EventBus.getDefault().post(new LocationEvent());
        }
    };
}
